package com.shangke.corejava.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//设计一个jdbc操作工具类，用于封装通用操作

public class DBHelper {
	//封装4个参数
	//声明为final 就是意味着变量不能修改，只能只读，也不能被重载
	private static final String DRIVER="oracle.jdbc.driver.OracleDriver";
	private static final String URL="jdbc:oracle:thin://localhost:1521/orcl";
	private static final String USERS="jdbcuser";
	private static final String PASSWORK="123456";
	
	//1.注册驱动；2.建立连接
	public static Connection openConnection() {
		Connection conn = null;
		
		try {
			Class.forName(DRIVER);
			conn=DriverManager.getConnection(URL, USERS, PASSWORK);
			if(conn!=null){
				System.out.println("连接成功");
			}else{
				System.out.println("连接失败");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	
	//6.关闭连接
	public static void closeAll(ResultSet rs,Statement stmt,Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				
			}
		}if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				
			}
		}
	}
}
